<template>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm"  label-position="top" label-width="80px" class="demo-ruleForm" size="large" >

    <el-form-item label="ProductID"  prop="productId">
      <el-input v-model="ruleForm.productId"style="width:300px"></el-input>
    </el-form-item>

    <el-form-item label="Category" prop="categoryId">
      <el-select v-model="ruleForm.categoryId" placeholder="请选择categoryId">
        <el-option v-for="category in categories"
                   :label="category.id"
                   :value="category.id"
        ></el-option>

      </el-select>
    </el-form-item>


    <el-form-item label="Name" prop="name">
    <el-input v-model="ruleForm.name"style="width:300px"></el-input>
   </el-form-item>


    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        productId: '',
        categoryId: '',
        name: '',

      },
      rules: {
        productId: [
          { required: true, message: '请输入正确的内容', trigger: 'blur' },
        ],
        categoryId: [
          { required: true, message: '请输入正确的内容', trigger: 'blur' },
        ],
        name: [
          { required: true, message: '请输入正确的内容', trigger: 'blur' },
        ],


      },
      categories:[{
        id:'BIRDS',

      }]
    };
  },
  created() {
    const _this = this
    //此处需要查询item的全部信息
    axios({
      method:"GET",
      url:'http://115.29.176.82:8081/manager/api/category',
      params:{
        size:500,
        page:1
      }
    }).then(function (resp){
      _this.categories = (resp.data.data.list)
    })

  },
  methods: {
    submitForm(formName) {
      const _this=this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(_this.ruleForm)
          axios({
            method:"POST",
            url:'http://115.29.176.82:8081/manager/api/product',
            data:{
              productId:_this.ruleForm.productId,
              categoryId:_this.ruleForm.categoryId,
              name:_this.ruleForm.name,
              description:' '
            }
          }).then(function (resp){
            console.log(resp)
            if(resp.status===200)
              alert('添加成功，请继续您的操作')
          })
        } else {
             alert('添加失败，请核实数据后，再次添加')
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>